package com.zxy.dept.service.impl;

import com.zxy.dept.domain.Department;
import com.zxy.dept.mapper.DepartmentMapper;
import com.zxy.dept.service.DepartmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 部门菜单树递归测试 和本项目无关 我只是写着玩玩
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> findAllMenu() {
        return getLevelList(departmentMapper.getTopDept());
    }

    private List<Department> getLevelList(List<Department> departments){
        ArrayList<Department> list = new ArrayList<>();
        departments.forEach(department -> {
            List<Department> childDept = departmentMapper.getChildDept(department.getDeptId());
            if (childDept.size()>0){
                getLevelList(childDept);
                department.setDepartments(childDept);
            }
            list.add(department);
        });
        return list;
    }
}
